/*****************************
This step takes all the graduates in the
all_students file, which have been flagged, and extracts the 
quarters that they graduated
******************************/
data cdhe_codes (keep=opeid_num InstitutionCode );
    infile "&codepath./cdhe_institutioncodes.csv" missover dsd delimiter=',' firstobs=2;
    format opeid_num best6.;
    format InstitutionCode $4.;
    format school_name $80.;
    format school_type $20.;
    format funding_type $20.;
    input opeid_num InstitutionCode school_name school_type funding_type $; 
    if opeid_num ne .  ;
run;

proc print data=cdhe_codes ; 
    title 'opeid crosswalk' ;
run;

proc sort data=cdhe_codes ; 
    by institutioncode;  
run;

data enrollment (keep = pik institutioncode year quarter qtime )
    /view=enrollment; 
    set INPUTS.coedu_enrollment_1997_2007_qpik
        INPUTS.coedu_enrollment_2008_2016_qpik ;
    
    
    
    year=reportyear ; 
    if reportterm = 2 then do ;
        quarter = 4 ;
    end;
    else if reportterm in (1,3,4,5) then do ;
        if reportterm=1 then quarter = 3 ;
        if reportterm in (3,5) then quarter = 1 ;
        if reportterm=4 then quarter = 2 ;
    end;
    
    
run;


proc sort data=enrollment out=enrollmentsort; 
    by pik institutioncode ; 
run;
    
proc sort data=OUTPUTS.all_students_cdhe out=all ; 
    by pik institutioncode ; 
run; 
    
data OUTPUTS.enrollment_long_cdhe ; 
    merge enrollmentsort (in=a) all (in=b) ;
    by pik institutioncode ; 
    
    if b ; 
    enroll= a ; 
    qtime = %qtime(year,quarter,location=datastep) ;
run;
    
proc sort data=OUTPUTS.enrollment_long_cdhe out=sample_flag ; 
    by pik institutioncode deglevl_code; 
run;

proc contents data=sample_flag  ;
run;

    

/***************************************
Making enrollment a wide file
*****************************************/
data OUTPUTS.enrollment_wide_cdhe (keep = pik opeid degcip_2dig
                credithours cte_flag flag_cte graduate enr61-enr138 state 
				deglevl_code qtime_grad ) ; 
    if _n_=0 then do ;
        set cdhe_codes (obs=0);
    end;
    if _n_=1 then do ;
        declare hash h_inst(dataset:'WORK.cdhe_codes',ordered:'Y') ;
        rc=h_inst.defineKey('institutioncode') ;
        rc=h_inst.defineData('institutioncode','opeid_num') ;
        rc=h_inst.defineDone() ;
        declare hiter i_inst('h_inst') ;
    end;
	
    length opeid $8. ;
	
    set sample_flag ; 
    by pik institutioncode ;
            
    retain enr21-enr138 ;
    array enr{21:138} enr21-enr138;  
    
    rc=h_inst.find();    
    opeid = put(opeid_num,z6.) ;
	opeid = trim(opeid)||'00' ;
	
    if first.institutioncode then do ; 
        do i = 21 to 138 ; 
            enr{i} = 0 ; 
        end;
    end;
	
    enr{qtime} = enroll ;  
            
    if last.pik then output ; 
run;

proc sort data=OUTPUTS.enrollment_wide_cdhe ;
        by pik opeid deglevl_code degcip_2dig ;
run;
